package com.zen.lon.open.proxy;

import com.zen.lon.open.remote.context.FactoryBeanContext;
import com.zen.lon.open.remote.context.AbstractFactoryBeanContextSupport;
import java.io.IOException;
import org.springframework.beans.factory.FactoryBean;

/**
 * spring 的 FactoryBean 用于依赖注入代理bean
 *
 * @author ZonLen since on 2021/6/30 下午1:47
 */
public class ProxyAgentFactoryBean<P> extends
    AbstractFactoryBeanContextSupport implements FactoryBean<P> {

  private FactoryBeanContext factoryBeanContext;

  private final Class<P> proxyClass;

  public ProxyAgentFactoryBean(Class<P> proxyClass) {
    this.proxyClass = proxyClass;
  }

  /**
   * 从上下文中获取代理对象
   */
  @Override
  public P getObject() throws ClassNotFoundException, IOException {
    return factoryBeanContext.newProxy(proxyClass);
  }

  @Override
  public Class<P> getObjectType() {
    return proxyClass;
  }

  @Override
  public void setFactoryBeanContext(FactoryBeanContext factoryBeanContext) {
    this.factoryBeanContext = factoryBeanContext;
  }

}
